<template>
    <el-form ref="formRef" style="max-width: 600px" :model="formData" :rules="rules" label-width="200px"
        class="demo-ruleForm" status-icon>
        <el-form-item label="商品名称" prop="name">
            <el-input v-model="formData.name" />
        </el-form-item>
        <el-form-item label="商品图片" prop="img">

            <Tuku v-model="formData.img1"></Tuku>
            <Tuku v-model="formData.img2"></Tuku>
            <Tuku v-model="formData.img3"></Tuku>
        </el-form-item>
        <el-form-item label="商品排序" prop="seq">
            <!-- <el-input v-model="formData.seq" /> -->
            <el-input-number v-model="formData.seq" :min="1" :max="10" />
        </el-form-item>
        <el-form-item label="商品价格" prop="price">

            <el-input-number v-model="formData.price" :precision="2" :step="0.1"/>
        </el-form-item>
        <el-form-item label="类目ID" prop="categoryId">
            <!-- <el-input v-model="formData.parentId" /> -->
            <el-tree-select v-model="formData.categoryId" :data="categoryData" check-strictly :render-after-expand="false"
                style="width: 240px" />
        </el-form-item>
          <el-form-item label="商品简介" prop="brief">
                    <!-- <el-input v-model="formData.brief" /> -->
                    <!-- <textarea v-model="formData.brief"></textarea> -->
                    <Editors v-model="formData.brief"></Editors>
                </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="submitForm(formRef)">
                提交
            </el-button>
            <el-button @click="resetForm(formRef)">重置</el-button>
        </el-form-item>
    </el-form>
</template>
<script setup lang="ts">
import { onMounted, reactive, ref } from 'vue'
import Tuku from '@/components/Tuku.vue'
import { CategoryApi ,ProductApi} from '@/api';
import { ElMessage } from 'element-plus'
import { useRoute, useRouter } from 'vue-router'
import Editors from '@/components/Editors.vue';
let route = useRoute() // 用来路由跳转的时候传递参数的
let router = useRouter() // 只是用来路由跳转

const formRef = ref()
const formData = reactive({
    name: '',
    seq: 0,
    img: "",
    img1: "",
    img2: "",
    img3: "",
    categoryId: 0,
    brief: '',
    price:0

})

const rules = reactive({
    name: [
        { required: true, message: '请输入类目的名称', trigger: 'blur' },
        { min: 2, max: 5, message: '类目名称不得小于2 大于5', trigger: 'blur' },
    ],
})

const categoryData = ref([
    {
        value: 0,
        label: '一级目录',
    }])


onMounted(() => {
    CallcategoryApi()
})

const CallcategoryApi = () => {
    CategoryApi.select.call().then((res: any) => {
        //console.log(res);
        //获取，名称和id值  对应的是列表里面的value和label
        res.forEach((item: any) => {
            item.value = item.id
            item.label = item.name
        });
        //高阶函数
        const categoryData2 = res.reduce((hezhi: any, currentValue: any) => {

            //console.log(currentValue);
            currentValue.children = res.filter((item: any) => {
                return item.parentId === currentValue.id
            })
            if (currentValue.parentId === 0) {
                hezhi.push(currentValue)
            }
            return hezhi
        }, [])
        console.log(categoryData2);
        categoryData.value = categoryData2

    })
}
//自己定义一个提交的方法 为了方便调试
const formsunbmit = () => {
    // formData.img = formData.img1 + "," + formData.img2 + "," + formData.img3
    let imgArray = [];
    if (formData.img1 != '') { imgArray.push(formData.img1) }
    if (formData.img2 != '') { imgArray.push(formData.img2) }
    if (formData.img3 != '') { imgArray.push(formData.img3) }
    formData.img=imgArray.join(',')
    ProductApi.insert.call({
        name: formData.name, img: formData.img, price: formData.price, brief: formData.brief,
        seq: formData.seq, categoryId: formData.categoryId
    }).then((res: any) => {
        console.log(res);

        ElMessage.success("添加成功")
        //跳转到列表页面
        router.push({ name: "product-list" })

    })
}



const submitForm = async (formEl: any) => {
    if (!formEl) return
    await formEl.validate((valid: any, fields: any) => {
        if (valid) {
            console.log('submit!')
            //回调函数
            formsunbmit()
        } else {
            console.log('error submit!', fields)
        }
    })
}

const resetForm = (formEl: any) => {
    if (!formEl) return
    formEl.resetFields()
}


</script>
<style scoped>
.demo-ruleForm {
    margin-top: 20px;
    margin-left: 20px;
}
</style>